﻿using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Xml.Linq;
using System;

using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.IO;
using System.Threading;
using System.Text;
using System.Threading.Tasks;
using System.Runtime;
using System.Security.Claims;
using System.Web;

class Program
{
    public static int 状态 = 0;

    static async Task Main(string[] args)
    {     
        string targetPath = "Survivalcraft.exe";
        string targetPath1 = "TerminalEntry.exe";
        // 启动目标程序
        ProcessStartInfo startInfo = new ProcessStartInfo
        {

            FileName = targetPath,
            // 如果需要，可以添加其他启动参数
            // Arguments = "your arguments here",
            UseShellExecute = false,
            CreateNoWindow = false // 根据需要设置
        };
        ProcessStartInfo server1 = new ProcessStartInfo
        {

            FileName = targetPath1,
            // 如果需要，可以添加其他启动参数
            // Arguments = "your arguments here",
            UseShellExecute = false,
            CreateNoWindow = false // 根据需要设置
        };

        Process process1 = new Process
        {
            StartInfo = server1,
            EnableRaisingEvents = true

        };

        Process process = new Process
        {
            StartInfo = startInfo,
            EnableRaisingEvents = true
        };

        // 启动程序
        if (File.Exists(targetPath1))
        {
            Program.状态 = 1;
            process1.Start();
        }
        else
        {
            Program.状态 = 0;
            Console.WriteLine("未找到服务端");
            Console.ReadKey();
        }

        if (File.Exists(targetPath))
        {
            process.Start();
        }
        else
        {
            Console.WriteLine("未找到进程");
            Console.ReadKey();
            Environment.Exit(0);
        }
        // 定义检测间隔、总超时时间和计数变量
        int checkInterval = 1000; // 每秒检查一次
        int totalTimeout = 30000; // 总超时时间为30秒
        int elapsed = 0;
        int unresponsiveCount = 0;

        while (true)
        {
            // 检查程序是否仍在运行
            if (process.HasExited)
            {
                Console.WriteLine("程序已退出。");
                break;
            }

            // 检查程序是否响应
            bool isResponding = true;
            try
            {
                isResponding = process.Responding;
            }
            catch (InvalidOperationException)
            {
                // 进程已经退出或无法访问
                Console.WriteLine("无法访问进程。");
                break;
            }

            if (!isResponding)
            {
                unresponsiveCount++;
                Console.WriteLine($"程序未响应。连续未响应次数: {unresponsiveCount}");
               
                // 可选：设定一个连续未响应的阈值，超过则终止程序
                int unresponsiveThreshold = 20; // 例如，连续13次未响应则终止
                if (unresponsiveCount >= unresponsiveThreshold)
                {
                  
                    Console.WriteLine("程序连续未响应次数超过阈值，终止程序。");
                    process.Kill();
                    if(状态==1)
                    {
                        process1.Kill();
                    }
                    Console.WriteLine("程序已被终止。");
                    Console.WriteLine("已重启");
                    await Main(args);
                    break;
                }
            }
            else
            {
                // 如果程序响应，重置未响应计数
                if (unresponsiveCount > 0)
                {
                
                    Console.WriteLine("程序恢复响应。");
                    unresponsiveCount = 0;
                }              
            }

            // 检查是否达到总超时时间
           

            // 等待下一次检查
            await Task.Delay(checkInterval);
            elapsed += checkInterval;
        }

        // 可选：等待程序完全退出
        
        process.WaitForExit();
        if(状态==1)
        {
            process1.WaitForExit();
        }
        
    }  
}